<?php
/**
 * Nweb Editor
 *
 * This script is protected by copyright. Its use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.editor.config
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     1.0 $Id$
 * @link        https://nweb-editor.googlecode.com
 * @link        http://editor.nweb.pl
 */

namespace pl\nweb\editor\config;

/**
 * Menadżer konfiguracji
 *
 * @category    source
 * @package     pl.nweb.editor.config
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @version     1.0 $Id$
 */
class Manager
{
    /**
     * Kolekcja obiektów klasy Zend_Config
     *
     * @var array
     */
    private static $_configs = array();

    /**
     * Zwraca obiekt klasy Zend_Config
     *
     * @return \Zend_Config
     */
    public static function getConfig ($name, $dir = null)
    {
        if(null === $dir) {
            $dir = APPLICATION_PATH . '/configs';
        }
        $file = $dir . DIRECTORY_SEPARATOR . $name;
        $key  = sha1($file);
        if(!isset(self::$_configs[$key])) {
            $cache = \pl\nweb\editor\cache\Manager::getConfig();
            if(($data = $cache->load($key)) === false) {
                $data = new \Zend_Config(include $file);
                $cache->save($data, $key);
            }
            self::$_configs[$key] = $data;
        }
        return self::$_configs[$key];
    }
}